package com.yg.controller;


import com.yg.config.result.R;
import com.yg.entity.readAndWriteTemplates.TeacherRead;
import com.yg.entity.singleEntity.Teacher;
import com.yg.service.TeacherService;
import com.yg.utils.CloseStream;
import com.yg.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * <p>
 * 对应sheet01和其他表，此表为教师表 前端控制器
 * </p>
 *
 * @author YingGu
 * @since 2020-11-04
 */
@RestController
@RequestMapping("/teacher")
@CrossOrigin
public class TeacherController {

    @Autowired
    TeacherService teacherService;

    @PostMapping("login")
    public R teacherLogin(@RequestBody Teacher teacher) {

        R flag = teacherService.teacherLogin(teacher);

        return flag;
    }

    /**
     * 查询所有
     * clr
     *
     * @return
     */
    @GetMapping("/selectAllTeacherInfo")
    public R selectAllTeacherInfo(HttpServletRequest httpServletRequest,@RequestParam("current") int current, @RequestParam("limit") int limit) {
        String vipByJwtToken = JwtUtils.getVipByJwtToken(httpServletRequest);
        String teacherIdByJwtToken = JwtUtils.getTeacherIdByJwtToken(httpServletRequest);
        R r = teacherService.selectAllTeacherInfo(current,limit,vipByJwtToken,teacherIdByJwtToken);
        return r;
    }

    /**
     * 新增单个老师
     * @param teacherRead
     * @return
     */
    @PostMapping("/insertTeacherInfo")
    public R insertTeacherInfo(@RequestBody TeacherRead teacherRead, HttpServletRequest httpServletRequest){
        String vipByJwtToken = JwtUtils.getVipByJwtToken(httpServletRequest);
        return "v1".equals(vipByJwtToken) ? teacherService.insertTeacherInfo(teacherRead) : R.ERROR().message("警告：请以管理员身份登录");
    }


    /**
     * 读取excel写入数据库
     * clr
     *
     * @param file
     * @return
     */
    @PostMapping("/readExcel")
    public R readExcel(MultipartFile file,HttpServletRequest httpServletRequest) {
        //获取fileInputStream
        FileInputStream fileInputStream = null;
        //判断权限
        String vipByJwtToken = JwtUtils.getVipByJwtToken(httpServletRequest);
        if ("v2".equals(vipByJwtToken)){
            return R.ERROR().message("警告：请以管理员身份登录");
        }
        R r = null;
        try {
            fileInputStream = (FileInputStream) file.getInputStream();
            r = teacherService.readExcel(fileInputStream);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭fileInputStream
            CloseStream.closeFileInputStream(fileInputStream);
        }
        return r;
    }


    /**
     * 导出excel
     * clr
     *
     * @return
     */
    @GetMapping("/writeExcel")
    public R writeExcel(HttpServletRequest httpServletRequest,HttpServletResponse response ,@RequestParam(value = "name",required = false)String name,@RequestParam(value = "title",required = false)String title,@RequestParam(value = "education",required = false)String education){
        String vipByJwtToken = JwtUtils.getVipByJwtToken(httpServletRequest);
        TeacherRead teacherRead = new TeacherRead();
        teacherRead.setName(name);
        teacherRead.setTitle(title);
        teacherRead.setEducation(education);
        String teacherIdByJwtToken = JwtUtils.getTeacherIdByJwtToken(httpServletRequest);
        R r = teacherService.writeExcel(response,teacherRead,vipByJwtToken,teacherIdByJwtToken);
        return r;
    }


    /**
     *  条件查询
     * @return
     */
    @PostMapping("/teacherConditionalQuery")
    public R teacherConditionalQuery(HttpServletRequest httpServletRequest,@RequestBody String teacherCondition,@RequestParam("current") int current,@RequestParam("limit") int limit){
        String vipByJwtToken = JwtUtils.getVipByJwtToken(httpServletRequest);
        String teacherIdByJwtToken = JwtUtils.getTeacherIdByJwtToken(httpServletRequest);
        R r = teacherService.teacherConditionalQuery(teacherCondition,current,limit,vipByJwtToken,teacherIdByJwtToken);
        return r;
    }

    /**
     * 根据id更新教师信息
     * @return
     */
    @PutMapping("/updateTeacherInfo")
    public R updateTeacherInfo(@RequestBody String teacherJson,HttpServletRequest httpServletRequest){
        String vipByJwtToken = JwtUtils.getVipByJwtToken(httpServletRequest);
        return "v1".equals(vipByJwtToken) ? teacherService.updateTeacherInfo(teacherJson) : R.ERROR().message("警告：请以管理员身份登录");
    }

    /**
     * 根据id删除教师信息
     * @param id
     * @return
     */
    @DeleteMapping("/deleteTeacherById")
    public R deleteTeacherById(@RequestBody String id,HttpServletRequest httpServletRequest){
        String vipByJwtToken = JwtUtils.getVipByJwtToken(httpServletRequest);
        return "v1".equals(vipByJwtToken) ? teacherService.deleteTeacherById(id) : R.ERROR().message("警告：请以管理员身份登录");
    }
}

